
#include <utest.h>

#include <thread>
#include <chrono>
#include <iostream>
#include <future>
#include <functional>

#include <hello/conc/executor.h>


int mult_func(int a, int b) {
    std::this_thread::sleep_for(std::chrono::seconds(2));
    return a * b;
}

TEST(test001, case001, false) {
    std::packaged_task<int()> pt1([] { return mult_func(12, 13); });
    std::future<int> ft1 = pt1.get_future();

    std::thread t1(std::move(pt1));

    std::cout << "Hello World" << std::endl;
    std::cout << ft1.get() << std::endl;
    std::cout << "over..." << std::endl;

    t1.join();
}

TEST(test001, case002, true) {
    hello::thread_pool tp(6, 32);

    std::vector<std::future<int>> futs;
    for (int i = 0; i < 39; i++) {
        futs.emplace_back(tp.submit(mult_func, i, i + 1));
    }
    for (int i = 0; i < 39; i++) {
        std::cout << futs[i].get() << std::endl;
    }
}

TEST(test001, case003, false) {
    hello::thread_pool tp(4, 10);

    auto fut1 = tp.submit(mult_func, 12, 13);
    auto fut2 = tp.submit(mult_func, 33, 12);
    auto fut3 = tp.submit(mult_func, 43, 556);
    auto fut4 = tp.submit(mult_func, 232, 321);
    auto fut5 = tp.submit(mult_func, 45, 11);
    auto fut6 = tp.submit(mult_func, 56, 44);
    auto fut7 = tp.submit(mult_func, 123, 11);
    auto fut8 = tp.submit(mult_func, 312312, 11);
    auto fut9 = tp.submit(mult_func, 33, 11);
    auto fut10 = tp.submit(mult_func, 111, 11);

    std::cout << fut1.get() << std::endl;
    std::cout << fut2.get() << std::endl;
    std::cout << fut3.get() << std::endl;
    std::cout << fut4.get() << std::endl;
    std::cout << fut5.get() << std::endl;
    std::cout << fut6.get() << std::endl;
    std::cout << fut7.get() << std::endl;
    std::cout << fut8.get() << std::endl;
    std::cout << fut9.get() << std::endl;
    std::cout << fut10.get() << std::endl;
}

